namespace :db do
  desc "Erase and fill database"
  task :populate => :environment do
    require 'populator'
    require 'faker'

    [Church].each(&:delete_all)
    [Person].each(&:delete_all)
    [PersonStatus].each(&:delete_all)

    Church.populate 50 do |church|
      church.name = Faker::Company.name + ' Church'
      church.address = Faker::Address.street_address
      church.city = Faker::Address.city
      church.state = Faker::Address.us_state_abbr
      church.zip_code = Faker::Address.zip_code
      church.phone = Faker::PhoneNumber.phone_number
      church.url = rand(100).odd? ? ("http://" + Faker::Internet.domain_name) : nil
    
      PersonStatus.populate 1 do |status|
        status.church_id = church.id
        status.name = "Visitor"
        status.description = rand(100).odd? ? Populator.sentences(1..2) : nil
        status.enabled = [true, false]
        @status_start_id = status.id
      end
      
      PersonStatus.populate 1 do |status|
        status.church_id = church.id
        status.name = "Member"
        status.description = rand(100).odd? ? Populator.sentences(1..2) : nil
        status.enabled = [true, false]
        @status_end_id = status.id
      end
    
      Person.populate 50 do |person|
        person.church_id = church.id
        person.first_name = Faker::Name.first_name
        person.last_name = Faker::Name.last_name
        person.address = Faker::Address.street_address
        person.city = Faker::Address.city
        person.state = Faker::Address.us_state_abbr
        person.zip_code = Faker::Address.zip_code
        person.email = rand(100).odd? ? Faker::Internet.email : nil
        person.phone = rand(100).odd? ? Faker::PhoneNumber.phone_number : nil
        person.note = rand(100).odd? ? Populator.sentences(2..15) : nil
        person.status_id = @status_start_id..@status_end_id
        person.enabled = [true, false]
      end
    end  
  end
end